<?php

class ZFEngine_View_Helper_DateDifference extends Zend_View_Helper_Abstract
{

   /**
     * Calc diff between two dates
     *
     * @param mix $date1
     * @param mix $date2
     * @return array
     */
    public function dateDifference($date1, $date2, $part = false)
    {
        $date1 = new Zend_Date($date1);
        $date1 = $date1->getTimestamp();

        $date2 = new Zend_Date($date2);
        $date2 = $date2->getTimestamp();

        $diff_secs = abs($date1 - $date2);
        $base_year = min(date("Y", $date1), date("Y", $date2));

        $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);

        $result = array(
            "years"                 => abs(substr(date('Ymd', $date1) - date('Ymd', $date2), 0, -4)),
            "months_total"  => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
            "months"                => date("n", $diff) - 1,
            "days_total"    => floor($diff_secs / (3600 * 24)),
            "days"                  => date("j", $diff) - 1,
            "hours_total"   => floor($diff_secs / 3600),
            "hours"                 => date("G", $diff),
            "minutes_total" => floor($diff_secs / 60),
            "minutes"               => (int) date("i", $diff),
            "seconds_total" => $diff_secs,
            "seconds"               => (int) date("s", $diff)
        );
        
        if ($part === false) {
            return $result;
        } else {
            return isset($result[$part]) ? $result[$part] : false ;
        }
    }

}